package com.winning.pmph.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public class KnowledgeSyncUtil {
		
	public static String parseKnowledgeFieldImageType(String property) {
		// { type: '', additional: '', classification: '', title: '', comment: '', paths: ['/ymer/action/file/showImage?filename=image/bz/pic1.jpeg', '/ymer/action/file/showImage?filename=image/bz/pic2.jpeg'] }
		if (StringUtils.isEmpty(property)){
			return "";
		}
		JSONObject propertyJson = JSONObject.parseObject(property);
		String type = propertyJson.getString("type");
		String additional = propertyJson.getString("additional");
		String classification = propertyJson.getString("classification");
		String title = propertyJson.getString("title");
		String comment = propertyJson.getString("comment");
		JSONArray paths = propertyJson.getJSONArray("paths");
		if (CollectionUtils.isEmpty(paths)){
			return "";
		}
		Element tempParent = new Element("div");
		// 如果是行内图, 只返回img标签
		if (type.contains("hnt")){
			return parsePathsToImg(paths, type, additional, classification);
		}
		// 获取第一张图片名称;
		String path = paths.getString(0);
		String imageFullName = path.substring(path.lastIndexOf("/") + 1);
		String imageName = imageFullName.substring(0, imageFullName.lastIndexOf("."));
		// 表题在上, 图片中间, 表注, 图题图注在下;
		if (StringUtils.isNotEmpty(title)
			&& Objects.equals("rw_img table", type)) {
			Element titleEl = new Element("p");
			titleEl.addClass("t_title");
			titleEl.attr("id", "t_title_" + imageName);
			titleEl.html(title);
			titleEl.appendTo(tempParent);
		}
		// 图片 path -> img标签
		Element imgParent = new Element("p");
		if (paths.size() > 1){
			imgParent.addClass("zt");
		}
		imgParent.html(parsePathsToImg(paths, type, additional, classification));
		imgParent.appendTo(tempParent);
		// 图题
		if (StringUtils.isNotEmpty(title)
				&& !Objects.equals("rw_img table", type)) {
			Element titleEl = new Element("p");
			titleEl.addClass("p_title");
			titleEl.attr("id", "p_title_" + imageName);
			titleEl.html(title);
			titleEl.appendTo(tempParent);
		}
		// 图注, 表注;
		if (StringUtils.isNotEmpty(comment)) {
			// 表注
			Element noteEl = new Element("p");
			if (Objects.equals("rw_img table", type)) {
				noteEl.addClass("t_note");
				noteEl.attr("id", "t_note_" + imageName);
			}
			// 图注
			else {
				noteEl.addClass("p_note");
				noteEl.attr("id", "p_note_" + imageName);
			}
			noteEl.html(comment);
			noteEl.appendTo(tempParent);
		}
		return tempParent.html();
	}

	private static String parsePathsToImg(JSONArray paths, String type, String additional, String classification){
		Element tempEl = new Element("div");
		for (int i = 0; i < paths.size(); i++) {
			String pathItem = paths.getString(i);
			Element imgEl = new Element("img");
			String imageItemFullName = pathItem.substring(pathItem.lastIndexOf("/") + 1);
			String imageItemName = imageItemFullName.substring(0, imageItemFullName.lastIndexOf("."));
			imgEl.attr("id", imageItemName);
			imgEl.addClass(type);
			imgEl.addClass(additional);
			imgEl.attr("data-additional", additional);
			imgEl.addClass(classification);
			imgEl.attr("data-classification", classification);
			imgEl.attr("src", pathItem);
			imgEl.appendTo(tempEl);
		}
		return tempEl.html();
	}

	public static String parseKnowledgeFieldVideoType(String property) {
		// { type: '', title: '', paths: [{ image: '/ymer/action/file/showImage?filename=image/bz/pic1.jpg', video: '/pmph/action/video/play?filename=video/video1.mp4' }, { image: '/ymer/action/file/showImage?filename=image/bz/pic2.jpg', video: '/pmph/action/video/play?filename=video/video2.mp4' }] }
		if (StringUtils.isEmpty(property)){
			return "";
		}
		JSONObject propertyJson = JSONObject.parseObject(property);
		String type = propertyJson.getString("type");
		String title = propertyJson.getString("title");
		JSONArray paths = propertyJson.getJSONArray("paths");
		if (CollectionUtils.isEmpty(paths)){
			return "";
		}
		// 获取第一个视频名称;
		String path = paths.getString(0);
		String videoFullName = path.substring(path.lastIndexOf("/") + 1);
		String videoName = videoFullName.substring(0, videoFullName.lastIndexOf("."));
		Element tempParent = new Element("div");
		// 视频标题;
		if (StringUtils.isNotEmpty(title)) {
			Element titleEl = new Element("p");
			titleEl.attr("id", "v_title_" + videoName);
			titleEl.attr("class", "v_title");
			titleEl.html(title);
			titleEl.appendTo(tempParent);
		}
		Element videoParent = new Element("p");
		if (paths.size() > 1){
//			videoParent.addClass("zt");
		}
		for (int i = 0; i < paths.size(); i++) {
			String videoItemPath = paths.getString(i);
			Element videoEl = new Element("video");
			String videoItemFullName = videoItemPath.substring(path.lastIndexOf("/") + 1);
			String videoItemName = videoItemFullName.substring(0, videoItemFullName.lastIndexOf("."));
			videoEl.attr("id", videoItemName);
			videoEl.attr("controls", "controls");
			videoEl.addClass(type);
			videoEl.attr("src", videoItemPath);
			// 不添加该属性,视频无法播放
			videoEl.attr("preload");
			videoEl.appendTo(videoParent);
		}
		videoParent.appendTo(tempParent);
		return tempParent.html();
	}


	public static String parseKnowledgeFieldAudioType(String property) {
		String htmlStr = parseKnowledgeFieldVideoType(property);
		Document parse = Jsoup.parse(htmlStr);
		parse.getElementsByTag("video").tagName("audio");
		return parse.body().html();
	}

	public static Object parseKnowledgeFieldSource(JSONArray source) {
		// source 数据库格式, [{"isbn":"asd","name":"123","version":"qwe"},{"isbn":"asd","name":"123","version":"qwe"}]
		//        同步格式    "<书名>,<版次>,<ISBN>|<书名2>,<版次2>,<ISBN2>"
		ArrayList<String> sourceStrArray = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(source)) {
			for (int j = 0; j < source.size(); j++) {
				JSONObject sourceJson = source.getJSONObject(j);
				sourceStrArray.add(sourceJson.getOrDefault("name", "") + "," + sourceJson.getOrDefault("version", "") + "," + sourceJson.getOrDefault("isbn", ""));
			}
		}
		return sourceStrArray.stream().collect(Collectors.joining("|"));
	}
}
